'use strict';

/* Directives */


angular.module('myApp.directives', [])
        .directive('appVersion', ['version', function(version) {
                return function(scope, elm, attrs) {
                    elm.text(version);
                };
            }])
        .directive('validatePassword', function() {
            return {
                require: 'ngModel',
                link: function(scope, elm, attrs, ctrl) {

                    var path = attrs.validatePassword.split('.');

                    ctrl.$parsers.unshift(function(viewValue) {

                        var compareValue = scope;
                        for (var i = 0; i < path.length; i++) {
                            compareValue = compareValue[path[i]];
                        }

                        if (viewValue == compareValue) {
                            // it is valid
                            ctrl.$setValidity('validate', true);
                            return viewValue;
                        } else {
                            // it is invalid, return undefined (no model update)
                            ctrl.$setValidity('validate', false);
                            return undefined;
                        }
                    });
                }
            };
        })
        .directive('menu', ['$rootScope', 'serviceMenu', 'serviceGame', 'serviceUser',
            function($rootScope, menu, game, user) {
                return {
                    restrict: 'E',
                    scope: {
                    },
                    link: function(scope, element, attrs) {
                        $rootScope.$watch(function () {
                                if (game.ownerUser) {
                                    return 'game';
                                }
                                if (user.email) {
                                    return 'user';
                                }
                                return 'login';
                            }, function(newValue, oldValue) {
                                
                            scope.user = user;
                            scope.type = newValue;
                            
                            if (newValue == 'user') {
                                scope.items = menu.afterLogin;
                            }
                            if (newValue == 'login') {
                                scope.items = menu.beforeLogin;
                            }
                            if (newValue == 'game') {
                                scope.items = menu.game;
                            }
                        });

                    },
                    templateUrl: 'partials/directives/menu.html',
                    replace: true
                };
            }])
        .directive('footer', ['$rootScope', 'serviceMenu', 'serviceGame', 'serviceUser',
            function($rootScope, menu, game, user) {
                return {
                    restrict: 'E',
                    scope: {
                    },
                    link: function(scope, element, attrs) {
                        
                        $rootScope.$watch(function() {
                            if (game.playerUser) {
                                return 'start';
                            }
                            if (game.ownerUser) {
                                return 'game';
                            }
                            return null;
                        }, function(newValue, oldValue) {
                            if (newValue) {
                                scope.type = newValue;
                                if (newValue == 'start') {
                                    if (user.email == game.ownerUser.email) {
                                        scope.oponent = game.playerUser;
                                    } else if (user.email == game.playerUser.email) {
                                        scope.oponent = game.ownerUser;
                                    } else {
                                        throw "system error";
                                    }
                                }
                            
                                scope.game = game;
                                scope.footerShow = true;
                            } else {
                                scope.footerShow = false;
                            }
                        });
                        
                        $rootScope.$watch(function() {
                            console.log(scope.footerShow, game.moveLocked);
                            if (scope.footerShow && game.moveLocked !== null) {
                                return game.moveLocked;
                            } 
                            return null;
                        }, function(newValue, oldValue) {
                            console.log(newValue);
                            if (newValue !== null) {
                                scope.game = game;
                            }
                        });
                        
                    },
                    templateUrl: 'partials/directives/footer.html',
                    replace: true
                };
            }])
        .directive('popup',
                function() {
                    return {
                        restrict: 'E',
                        scope: {},
                        link: function(scope, element, attrs) {
                            if (typeof attrs.data !== undefined) {
                                var data = angular.fromJson(attrs.data);
                                // do something with data
                                scope.title = data.title || 'Title';
                                scope.info  = data.info || 'Information';
                                scope.okBtnId = data.okBtnId || false;
                                scope.okBtnText = data.okBtnText || 'Ok';
                                
                                scope.cancelBtnText = data.cancelBtnText || 'Cancel';
                                
                                scope.infoAddId = data.infoAddId || false;
                            }
                        },
                        templateUrl: 'partials/directives/popup.html',
                        replace: true
                    };
                });
